package com.lnking.lnkingplace.config;

import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.user.UserDto;
import com.lnking.lnkingplace.service.IUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class TokenFilter implements Filter {

    @Resource
    UserUtil userUtil;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String url = request.getRequestURI();

        UserDto user = userUtil.getUser();

        List<String> urlList = new ArrayList<>();
        urlList.add("/login");
        urlList.add("/register");
        urlList.add("/isolation-personnel/updateRoom");
        urlList.add("/record-monitor/add");
        urlList.add("/record-environment-sterilize/add");
        urlList.add("/isolation-personnel/add");
        urlList.add("/isolation-personnel/update");
//        urlList.add("/isolation-personnel/all/list");
        urlList.add("/record-medical-waste-sterilize/add");
        urlList.add("/record-sewage/add");
        urlList.add("/isolation-worker/info-list");

        //放行接口
        if(urlList.contains(url)){
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else{
            if(null == user){
                ReturnResult.error("没有token");
            }
            else{
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }

    }
}
